package com.qijia.travel.app;

import java.util.List;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.Status;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.StatusExample;

public class StatusManager {
	private BaseDAO baseDao;

	public StatusManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public Status getStatusById(int statusId) throws AppErrorException {
		Status status = new Status();
		status.setStatusId(statusId);

		try {
			return (Status) baseDao.selectByPrimaryKey(SqlMappedId.STATUS + "."
					+ SqlMappedId.selectByPrimaryKey, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取导游基本信息失败!");
		}
	}

	public void createStatus(Status status) throws AppErrorException {
		if (status == null)
			throw new AppErrorException("请输入新增的状态名称的基本信息");
		try {
			if (status.getStatusName() != null)
				baseDao.insert(SqlMappedId.STATUS + "." + SqlMappedId.insert,
						status);
		} catch (TravelDbException e) {
			throw new AppErrorException("注册新状态名称失败,请稍候重试!", e);
		}
	}

	public void updateStatus(Status status) throws AppErrorException {
		if (status == null)
			throw new AppErrorException("请选择要进行修改的状态名称");
		try {
			baseDao.updateByPrimaryKeySelective(SqlMappedId.STATUS + "."
					+ SqlMappedId.updateByPrimaryKeySelective, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("更新选中的状态名称[" + status.getStatusName()
					+ "]失败,请稍候重试!", e);
		}
	}

	public void deleteStatus(int statusId) throws AppErrorException {
		if (statusId == 0)
			throw new AppErrorException("请选择要进行删除的状态名称");
		Status status = new Status();
		try {
			status.setStatusId(statusId);
			baseDao.deleteByPrimaryKey(SqlMappedId.STATUS + "."
					+ SqlMappedId.deleteByPrimaryKey, status);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的状态名称[编号为:" + status.getStatusId()
					+ "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Status> getStatusByExample() throws AppErrorException {
		StatusExample example = ExampleFactory.createStatusExample();
		example.createCriteria();
		try {
			return (List<Status>) baseDao.selectByExample(SqlMappedId.STATUS
					+ "." + SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的状态名称失败,请稍候重试!", e);
		}
	}

	public PageResult selectStatusList(int curPage) throws AppErrorException {
		StatusExample example = ExampleFactory.createStatusExample();
		example.createCriteria();
		example.setOrderByClause(" status_id ");

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.STATUS + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.STATUS + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(20);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取状态名称数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取状态名称数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取状态名称数据列表失败,请稍候重试!", e);
		}
	}
}
